Advertisement
CodingComputing

Lock Code Cracker

May 20th, 2024
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. # Lock code cracker by @CodingComputing
  2.  
  3. def count_correct(com, ref):
  4.     return len([a for a in com if a in ref])
  5.  
  6. def count_correct_well_placed(com, ref):
  7.     return len([a for a,b in zip(com,ref) if a==b])
  8.  
  9. def count_correct_wrongly_placed(com, ref):
  10.     n_correct = count_correct(com, ref)
  11.     n_well_placed = count_correct_well_placed(com, ref)
  12.     return n_correct - n_well_placed
  13.  
  14. condition_checkers = [
  15.     lambda com: count_correct_well_placed(com, '682') == 1,
  16.     lambda com: count_correct_wrongly_placed(com, '614') == 1,
  17.     lambda com: count_correct_wrongly_placed(com, '206') == 2,
  18.     lambda com: count_correct(com, '738') == 0,
  19.     lambda com: count_correct_wrongly_placed(com, '780') == 1,
  20. ]
  21.  
  22. def check_all(com):
  23.     return all([check(com) for check in condition_checkers])
  24.  
  25. valid_coms = []
  26. for num in range(1000):
  27.     com = f"{num:03}"
  28.     if check_all(com):
  29.         valid_coms.append(com)
  30.  
  31. print("Valid combinations:", valid_coms)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement